package com.dzjcloud.dzj.upms.biz.controller.sys;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.dzjcloud.dzj.common.core.constant.CommonConstants;
import com.dzjcloud.dzj.common.core.enums.ResultEnum;
import com.dzjcloud.dzj.common.core.utils.GetTime;
import com.dzjcloud.dzj.common.core.utils.R;
import com.dzjcloud.dzj.common.core.exception.HandleException;
import com.dzjcloud.dzj.upms.api.entity.Menu;
import com.dzjcloud.dzj.upms.biz.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单权限表(Menu)表控制层
 *
 * @author makejava
 * @since 2021-06-08 17:18:42
 */
@RestController
@RequestMapping(CommonConstants.SYS+"/menu")
@Api(tags = "菜单权限前端控制器",value = "菜单权限前端控制器")
@Slf4j
@Validated
@RequiredArgsConstructor
public class SysMenuController {

    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;



    /**
     * 新增数据
     *
     * @param menu 实体对象
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation("新增菜单")
    public R insert(@RequestBody Menu menu) {
        if (StrUtil.isBlank(menu.getParentId())){
            throw new HandleException(ResultEnum.PARAM_IS_ERROR.getCode(),"上级Id不能为空");
        }else if(StrUtil.isBlank(menu.getName())){
            throw new HandleException(ResultEnum.PARAM_IS_ERROR.getCode(),"名称不能为空");
        }else if(StrUtil.isBlank(menu.getPermission())&&StrUtil.isBlank(menu.getPath())){
            throw new HandleException(ResultEnum.PARAM_IS_ERROR.getCode(),"权限标识或者菜单URL不能为空");
        }else if(ObjectUtil.isNull(menu.getSort())){
            throw new HandleException(ResultEnum.PARAM_IS_ERROR.getCode(),"排序号不能为空");
        }else if(ObjectUtil.isNull(menu.getType())){
            throw new HandleException(ResultEnum.PARAM_IS_ERROR.getCode(),"菜单类型不能为空");
        }
        menu.setId(GetTime.getGuid());
        return  R.ok(this.menuService.save(menu));
    }


    /**
     * 修改数据
     *
     * @param menu 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation("修改菜单")
    public R update(@RequestBody Menu menu) {
        if (StrUtil.isBlank(menu.getParentId())){
            throw new HandleException(ResultEnum.PARAM_IS_ERROR.getCode(),"上级Id不能为空");
        }else if(StrUtil.isBlank(menu.getName())){
            throw new HandleException(ResultEnum.PARAM_IS_ERROR.getCode(),"名称不能为空");
        }else if(StrUtil.isBlank(menu.getPermission())&&StrUtil.isBlank(menu.getPath())){
            throw new HandleException(ResultEnum.PARAM_IS_ERROR.getCode(),"权限标识或者菜单URL不能为空");
        }else if(ObjectUtil.isNull(menu.getSort())){
            throw new HandleException(ResultEnum.PARAM_IS_ERROR.getCode(),"排序号不能为空");
        }else if(ObjectUtil.isNull(menu.getType())){
            throw new HandleException(ResultEnum.PARAM_IS_ERROR.getCode(),"菜单类型不能为空");
        }
        return  R.ok(this.menuService.updateById(menu));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("删除菜单")
    public R delete(@Param("主键")@PathVariable String id) {
        return  R.ok(this.menuService.removeById(id));
    }


    @GetMapping("/tree")
    @ApiOperation("书查询菜单")
    public R<List<Tree<String>>> getTree(){
        return R.ok(menuService.getTree());
    }
}
